//
//  SecondViewController.m
//  MagicMoveDemo
//
//  Created by 卢小辉 on 15/10/14.
//  Copyright © 2015年 lulee007. All rights reserved.
//

#import "SecondViewController.h"
#import "FirstCollectionViewController.h"
#import "MagicMoveInverseTransition.h"
@interface SecondViewController ()
@property (nonatomic, strong) UIPercentDrivenInteractiveTransition *percentDrivenTransition;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.delegate=self;
    
    UIScreenEdgePanGestureRecognizer *edgePanGestureRecognizer=[[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(edgePanGesture:)];
    edgePanGestureRecognizer .edges=UIRectEdgeLeft;
    [self.view addGestureRecognizer:edgePanGestureRecognizer];
    
}

-(void)edgePanGesture:(UIScreenEdgePanGestureRecognizer *)recognizer{
    CGFloat progress=[recognizer translationInView:self.view].x/(self.view.bounds.size.width*1.0);
    NSLog(@"%f",progress);
//    限制到0-1之间
    progress= MAX(0.0, MIN(1.0, progress));
    if(recognizer.state==UIGestureRecognizerStateBegan){
        self.percentDrivenTransition=[[UIPercentDrivenInteractiveTransition alloc]init];
        [self.navigationController popViewControllerAnimated:YES];
    }else if(recognizer.state==UIGestureRecognizerStateChanged){
        [self.percentDrivenTransition updateInteractiveTransition:progress];
    }else if(recognizer.state==UIGestureRecognizerStateCancelled
             || recognizer.state==UIGestureRecognizerStateEnded){
        if(progress>0.1f){
            [self.percentDrivenTransition finishInteractiveTransition];
        }else {
            [self.percentDrivenTransition cancelInteractiveTransition];
        }
        self.percentDrivenTransition=nil;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{
    if([toVC isKindOfClass:[FirstCollectionViewController class]]){
        MagicMoveInverseTransition *transition=[[MagicMoveInverseTransition alloc]init];
        return transition;
    }else{
        return nil;
    }
}

- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController{
    if([animationController isKindOfClass:[MagicMoveInverseTransition class]]){
        return  self.percentDrivenTransition;
    }else{
        return nil;
    }
}



@end
